package leecode.q203;

public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode cur = head;
        ListNode dummy = new ListNode(0, head);
        ListNode pre = dummy;

        while (cur != null) {
            ListNode next = cur.next;
            if (cur.val == val) {
                pre.next = cur.next;
            } else {
                pre = cur;
            }
            cur = next;
        }
        return dummy.next;
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

}
